﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business;
using System.Text;
using NHibernate.Criterion;
using NHibernate;
using System.Collections;
using ZLCMS.Data.Model;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Config;
using Newtonsoft.Json;
using ZLCMS.Business.Util;

namespace ZLCMS.Web.SystemManage.Weixin
{
    public partial class WordMenuList : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        public string _s_title;

        protected string _userAccount;

        protected void Page_Load(object sender, EventArgs e)
        {
            _userAccount = UserKit.GetUserAccountFailLogin();

            //初始化全局变量值
            _s_title = Request["s_title"] == null ? "" : Request["s_title"];

            ////初始化控件值

            tb_search_title.Text = _s_title;

            //初始化DWZ分页控件
            _framePage = new FrameHelper(this) { RecordCount = GetListCount() };

            BindList(_framePage.PageIndex, _framePage.PageSize);
        }

        /// <summary>
        /// 填充网格数据
        /// </summary>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小</param>
        protected void BindList(int pageIndex, int pageSize)
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ICriteria criteria = session.CreateCriteria<WxResponseRule>();
                SetupCondition(ref criteria);
                criteria.AddOrder(Order.Asc("RequestKeyword")).SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize);

                grid.DataSource = criteria.List();
                grid.DataBind();
            }
        }

        /// <summary>
        /// 填充查询条件
        /// </summary>
        /// <param name="criteria"></param>
        private void SetupCondition(ref ICriteria criteria)
        {
            criteria.Add(Expression.Eq("OwnerAccount", _userAccount));//
            criteria.Add(Expression.Eq("Kind", "MENU"));//

            if (!string.IsNullOrEmpty(_s_title))
            {
                criteria.Add(Expression.Like("RuleTitle", "%" + _s_title + "%"));//是否确认订单
            }
        }

        /// <summary>
        /// 查询数据总数
        /// </summary>
        /// <returns></returns>
        protected int GetListCount()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ICriteria criteria = session.CreateCriteria<WxResponseRule>();
                SetupCondition(ref criteria);
                object obj = criteria.SetProjection(Projections.RowCount()).UniqueResult();
                return int.Parse(obj.ToString());
            }
        }

        public string GetMaterial(object item)
        {
            string result = "";
            WxResponseRule rule = item as WxResponseRule;

            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                WxMaterial material =session.CreateCriteria<WxMaterial>().Add(Expression.Eq("Id",rule.ResponseMaterialId)).Add(Expression.Eq("OwnerAccount", _userAccount)).UniqueResult<WxMaterial>();

                if (material == null)
                {
                    return "";
                }
                result = material.Title;
            }
            return result;
        }
    }
}
